package com.wl.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wl.entity.Material;
import com.wl.service.MaterialService;
import com.wl.util.PageData;

@Controller
public class MaterialController {

	@Autowired
	private MaterialService materialService;

	/**
	 * 根据id查找
	 * 
	 * @param id
	 * @return
	 */
	@GetMapping(value = "getmatbyid/{id}", produces = "application/json;charset=utf-8")
	@ResponseBody
	public Material selectByPrimaryKey(@PathVariable("id") Integer id) {
		Material material = materialService.selectByPrimaryKey(id);
		return material;
	}

	/**
	 * 物料查询分页显示
	 * 
	 * @param page     默认为1
	 * @param limit    默认为10
	 * @param keywords 默认为""
	 * @return
	 */
	@GetMapping(value = "getmatbylike", produces = "application/json;charset=utf-8")
	@ResponseBody
	public PageData<Material> selectMaterialByLike(@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "limit", defaultValue = "10") int limit,
			@RequestParam(value = "keywords", defaultValue = "") String keywords,
			@RequestParam(value = "materialid", defaultValue = "") String materialid,
			@RequestParam(value = "minprice", defaultValue = "") Double minprice,
			@RequestParam(value = "maxprice", defaultValue = "") Double maxprice,
			@RequestParam(value = "materialstatus", defaultValue = "") Integer materialstatus) {
		// page默认为1；limit默认为10，keywords默认为""
		PageData<Material> pageData = materialService.selectMaterialByLike(page, limit, keywords,materialid,minprice,maxprice,materialstatus);
		return pageData;
	}

	/**
	 * 根据id删除
	 * 
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "delmatbyid/{id}", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean deleteByPrimaryKey(@PathVariable("id") Integer id) {
		boolean flag = materialService.deleteByPrimaryKey(id);
		return flag;
	}

	/**
	 * 添加物料
	 * 
	 * @param record
	 * @return
	 */
	@PostMapping(value = "addmat", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean insert(@RequestBody Material record) {
		//获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String datetimenow = df.format(new Date());
		// String date=record.getMaterialdate();
		// System.out.println("前台时间"+date);
		// 将当系统日期和时间赋值给对象
		record.setMaterialdate(datetimenow);
		// 调用insert()方法
		boolean flag = materialService.insert(record);
		return flag;
	}

	/**
	 * 根据物料编号查找物料
	 * 
	 * @param materialid
	 * @return
	 */
	@GetMapping(value = "getmatbymatid/{materialid}", produces = "application/json;charset=utf-8")
	@ResponseBody
	public Material selectByMaterialId(@PathVariable("materialid") String materialid) {
		Material material = materialService.selectByMaterialId(materialid);
		return material;
	}

	/**
	 * 根据id动态修改物料信息
	 * 
	 * @param record
	 * @return
	 */
	@PutMapping(value = "updmatbyid", produces = "application/json;charset=utf-8")
	@ResponseBody
	public boolean updateByPrimaryKeySelective(@RequestBody Material record) {
		//获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		String datetimenow = df.format(new Date());
		// 将当系统日期和时间赋值给对象
		record.setMaterialdate(datetimenow);
		// 调用updateByPrimaryKeySelective()方法
		boolean flag = materialService.updateByPrimaryKeySelective(record);
		return flag;
	}
}
